Component({
  properties: {
    options: {
      type: Array,
      value: []
    },
    // defaultOption: {
    //   type: Object,
    //   value: {
    //     id: '000',
    //     name: '123'
    //   }
    // },
    key: {
      type: String,
      value: 'id'
    },
    text: {
      type: String,
      value: 'name'
    },
    value:{
      type: String,
      value:''
    },
    xid:{
      type: String,
      value:''
    },
    position:{
      type:String,
      value:'bottom'
    }
  },
  data: {
    result: [],
    isShow: false,
    current: {},
    cacheList:[],
    bottomVal:"-240"
  },
  methods: {
    // inputFocus(e){
    //   var that = this
    //   var bottomVal = 0
    //   if(that.data.position=="top"){
        
    //     // bottomVal = e.detail.height+that.data.bottomVal
    //     bottomVal = Number(that.data.bottomVal)+330
    //     console.log(e,e.detail.height,bottomVal)
    //     that.setData({
    //       bottomVal:bottomVal
    //     })
    //   }
    // },
    // inputBlur(e){
    //   console.log(e)
    //   var that = this
    //   if(that.data.position=="top"){
    //     that.setData({
    //       bottomVal:"-240"
    //     },function(){
    //       console.log(this.data.bottomVal)
    //     })
    //   }
    // },
    optionTap(e) {
      let dataset = e.target.dataset
      this.setData({
        current: dataset,
        isShow: false
      });
      console.log(e.target.dataset)
      this.triggerEvent("change", e.target.dataset)
    },
    openClose() {
      this.setData({
        isShow: !this.data.isShow
      })
    },
    // 此方法供父组件调用
    close() {
      this.setData({
        isShow: false
      })
    },
    searchJob: function(event){
      // 调用父组件方法，并传参
      // this.triggerEvent("change", { ...dataset })
      this.triggerEvent("change", {name:event.detail.value,id:''})
      //event.detail.value
      this.setData({
        isShow: false
      })
      let that = this
      let arr = []
      let value = event.detail.value
      that.data.cacheList.forEach((item)=>{
        if(item.name.indexOf(value) !== -1){
          arr.push({
            id:item.id,
            name:item.name,
          })
        }
      })
      this.setData({
        result: arr,
      })
      this.setData({
        isShow: true,
      })
    }
  },
  observers:{
    // value:function(){
    //   console.log(this.data.value)
    // }
  },
  lifetimes: {
    attached() {
      // 接受默认value 保存并触发父组件方法
      this.setData({
        current:{
          name:this.data.value,
        }
      })
      if(this.data.value&&this.data.xid){
        this.triggerEvent("change", {name:this.data.value,id:this.data.xid})
      }else if(this.data.value){
        this.triggerEvent("change", {name:this.data.value,id:""})
      }
      // 属性名称转换, 如果不是 { id: '', name:'' } 格式，则转为 { id: '', name:'' } 格式
      let result = []
      if (this.data.key !== 'id' || this.data.text !== 'name') {       
        for (let item of this.data.options) {
          let { [this.data.key]: id, [this.data.text]: name } = item
          result.push({ id, name })
        }
        this.setData({
          // current: Object.assign({}, this.data.defaultOption),
          result: result,
          cacheList:result
        })
      }else{
        this.setData({
          cacheList: this.data.options,
          result: this.data.options
        })
      }
    }
  },
})